import { currentDates } from '../../utils/util'
Page({
  data: {
    minDate: new Date(1980, 1, 1).getTime(),
    maxDate: new Date().getTime(),
    currentDate: '',
    showTime: false,
    activeNames: [],
  },
  onLoad() {
    this._getUser()
  },
  // 选择地址的显示
  onChangeAddress({ detail }) {
    this.setData({
      activeNames: detail,
    })
  },
  // 确认地址
  addressBtn(e) {
    this.setData({
      activeNames: [],
    })
  },
  // 地址的变化
  onChangeInput({ detail }) {
    this.setData({
      address: detail,
    })
  },
  // 修改性别
  onChangeBirthday({ detail }) {
    this.setData({
      sex: detail,
    })
  },
  // 出生日期
  onInput({ detail }) {
    console.log(detail)

    this.setData({
      birthday: detail,
      currentDate: currentDates(detail, 1),
    })
  },
  //打开折叠面板
  _openColl({ detail }) {
    if (detail == 2) {
      this.setData({
        showTime: true,
      })
    }
  },
  // 确认时间
  _gettime() {
    this.setData({ showTime: false })
  },
  //关闭时间
  onCloseTime() {
    this.setData({ showTime: false })
  },
  // 修改用户信息
  async submit() {
    let { sex, birthday, address } = this.data
    let { code } = await wx.requestAsync({
      url: '/user/info/edit',
      data: {
        sex,
        birthday: birthday / 1000,
        address,
      },
    })
    if (code == 0) {
      wx.switchTab({
        url: '/pages/my/my',
      })
      wx.showToast({
        title: '修改成功',
        icon: 'none',
      })
    }
  },
  // 获取用户信息
  async _getUser() {
    let { code, data } = await wx.requestAsync({
      url: '/user/info',
    })
    if (code == 0) {
      data.birthday = data.birthday * 1000
      data.currentDate = currentDates(data.birthday, 1)
      this.setData(data)
    }
  },
  _tolink() {
    wx.navigateTo({
      url: '/pages/babyonly/babyonly',
    })
  },
})
